/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rpt.lib.relatorio;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import java.sql.*;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignParameter;
import net.sf.jasperreports.engine.design.JRDesignStyle;

/**
 *
 * @author Roberto
 */

public class CRelatorioJasperAncestral {

    JasperDesign fJasperDesign = null;

    public CRelatorioJasperAncestral() {
        super();
    }

    public void getCabecalho() throws JRException {
        //if (fCabecalho == null) ;
    }

    public void getRelatorio() {
    }

    public void getRelatorio4() {
    }
/*
private void getReport(
         GenericReportRequest pGenericReportRequest) {
      UserAttributeDetail lUserAttributeDetail = pGenericReportRequest
            .getUserAttribute();
      UserInfoDetail lUserInfoDetail = pGenericReportRequest.getUserInfo();
      AttributeDetail[] lAttributeDetailArray = lUserAttributeDetail
            .getAttributeDetailArray();
      UserDetails[] lUserDetailsArray = lUserInfoDetail.getUserDetailsArray();

      DynamicReportBuilder lDynamicReportBuilder = new DynamicReportBuilder();
      Style lDetailStyle = new Style();
      Style lHeaderStyle = new Style();
      Style lTitleStyle = new Style();
      Style lSubtitleStyle = new Style();
      Style lAmountStyle = new Style();
      lAmountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
      try {
         lDynamicReportBuilder.setTitle("Delfigo System Generated Report")
               .setSubtitle(
                     "The items in this report correspond "
                           + "to the user information")
               .setDetailHeight(15).setMargins(30, 20, 30, 15)
               .setDefaultStyles(lTitleStyle, lSubtitleStyle,
                     lHeaderStyle, lDetailStyle).setColumnsPerPage(1)
               .setPrintBackgroundOnOddRows(true)
               .setUseFullPageWidth(true);
         // Column definitions. We use a new ColumnBuilder instance for each
         // column, the ColumnBuilder.getInstance() method returns a new
         // instance of the builder
         AbstractColumn lColumnState = null;
         for (UserDetails lUserDetails : lUserDetailsArray) {
            lColumnState = ColumnBuilder.getInstance().setColumnProperty(
                  "m" + lUserDetails.getUserProperty(),
                  String.class.getName()).setTitle(
                  lUserDetails.getUserProperty()).setWidth(50).build();
            // We add the columns to the report (through the builder) in the
            // order we want them to appear
            lDynamicReportBuilder.addColumn(lColumnState);
         }
         for (AttributeDetail lAttributeDetail : lAttributeDetailArray) {
            lColumnState = ColumnBuilder.getInstance().setColumnProperty(
                  "m" + lAttributeDetail.getAttributeName(),
                  String.class.getName()).setTitle(
                  lAttributeDetail.getAttributeName()).setWidth(50)
                  .build();
            // We add the columns to the report (through the builder) in the
            // order we want them to appear
            lDynamicReportBuilder.addColumn(lColumnState);
         }
         // we tell the report to use the full width of the page. this
         // resizes the columns width proportionally to meat the page width.
         lDynamicReportBuilder.setUseFullPageWidth(true);
      } catch (Exception lE) {
         lE.printStackTrace();
      }
      DynamicReport lDynamicReport = lDynamicReportBuilder.build();

      return;
    }
 */
    public static void runReport(Connection aConnection, String databaseName, String userName, String password,String reportFile) {
        try{
               JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
               JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
               //Connection jdbcConnection = connectDB(databaseName, userName, password);
               JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, aConnection);
               JasperViewer.viewReport(jasperPrint);
         }catch(Exception ex) {
               String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
               System.out.println(connectMsg);
         }
    }
    public static Connection connectDB(String databaseName, String userName, String password) {
        Connection jdbcConnection = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String vConexao =
                "jdbc:mysql://sin?user=root&password=adminadmin";
            jdbcConnection = DriverManager.getConnection(vConexao);
//            jdbcConnection = DriverManager.getConnection(databaseName,userName,password);
        } catch(Exception ex) {
            String connectMsg = "Could not connect to the database: " + ex.getMessage() + " " + ex.getLocalizedMessage();
            System.out.println(connectMsg);
        }
        return jdbcConnection;
    }
    
    public void getDesign() {
     JasperDesign jasperDesign = new JasperDesign ();
      jasperDesign.setName("SampleReport");
      jasperDesign.setPageWidth(550);
      jasperDesign.setPageHeight(800);
      jasperDesign.setColumnWidth(515);
      jasperDesign.setColumnSpacing(0);
      jasperDesign.setLeftMargin(40);
      jasperDesign.setRightMargin(40);
      jasperDesign.setTopMargin(50);
      jasperDesign.setBottomMargin(50);        
    }
    
    public JRDesignStyle getEstiloDesign(JasperDesign aJasperDesign) {
        JRDesignStyle normalStyle = new JRDesignStyle();
        normalStyle.setName("Arial_Normal");
        normalStyle.setDefault(true);
        normalStyle.setFontName("Arial");
        normalStyle.setFontSize(12);
        normalStyle.setPdfFontName("Helvetica");
        normalStyle.setPdfEncoding("Cp1252");
        normalStyle.setPdfEmbedded(false);
        if (aJasperDesign != null)
            try {
                aJasperDesign.addStyle(normalStyle);
            } catch (JRException ex) {
                Logger.getLogger(CRelatorioJasperAncestral.class.getName()).log(Level.SEVERE, null, ex);
            }
        return normalStyle;
    };

    public JRDesignStyle getEstiloDesignBold(JasperDesign aJasperDesign) {
        JRDesignStyle boldStyle = new JRDesignStyle();
        boldStyle.setName("Arial_Bold");
        boldStyle.setFontName("Arial");
        boldStyle.setFontSize(12);
        boldStyle.setBold(true);
        boldStyle.setPdfFontName("Helvetica-Bold");
        boldStyle.setPdfEncoding("Cp1252");
        boldStyle.setPdfEmbedded(false);
        try {
            aJasperDesign.addStyle(boldStyle);
        } catch (JRException ex) {
            Logger.getLogger(CRelatorioJasperAncestral.class.getName()).log(Level.SEVERE, null, ex);
        }
        return boldStyle;
    }

    public JRDesignParameter parametroDesign(JasperDesign aJasperDesign) {
        JRDesignParameter parameter = new JRDesignParameter();
        parameter.setName("ReportTitle");
        parameter.setValueClass(java.lang.String.class);
        try {
            aJasperDesign.addParameter(parameter);
        } catch (JRException ex) {
            Logger.getLogger(CRelatorioJasperAncestral.class.getName()).log(Level.SEVERE, null, ex);
        }
        return parameter;
    }

/*

    JRDesignStyle italicStyle = new JRDesignStyle();
    italicStyle.setName("Arial_Italic");
    italicStyle.setFontName("Arial");
    italicStyle.setFontSize(12);
    italicStyle.setItalic(true);
    italicStyle.setPdfFontName("Helvetica-Oblique");
    italicStyle.setPdfEncoding("Cp1252");
    italicStyle.setPdfEmbedded(false);
    jasperDesign.addStyle(italicStyle

    //Parameters

    JRDesignParameter parameter = new JRDesignParameter();
    parameter.setName("ReportTitle");
    parameter.setValueClass(java.lang.String.class);
    jasperDesign.addParameter(parameter);

    parameter = new JRDesignParameter();
    parameter.setName("OrderByClause");
    parameter.setValueClass(java.lang.String.class);
    jasperDesign.addParameter(parameter);

    //Query
    JRDesignQuery query = new JRDesignQuery();
    query.setText("SELECT * FROM Address $P!{OrderByClause}");
    jasperDesign.setQuery(query);

    //Fields
    JRDesignField field = new JRDesignField();
    field.setName("Id");
    field.setValueClass(java.lang.Integer.class);
    jasperDesign.addField(field);

    field = new JRDesignField();
    field.setName("FirstName");
    field.setValueClass(java.lang.String.class);
    jasperDesign.addField(field);

    field = new JRDesignField();
    field.setName("LastName");
    field.setValueClass(java.lang.String.class);
    jasperDesign.addField(field);

    field = new JRDesignField();
    field.setName("Street");
    field.setValueClass(java.lang.String.class);
    jasperDesign.addField(field);

    field = new JRDesignField();
    field.setName("City");
    field.setValueClass(java.lang.String.class);
    jasperDesign.addField(field);

    //Variables
    JRDesignVariable variable = new JRDesignVariable();
    variable.setName("CityNumber");
    variable.setValueClass(java.lang.Integer.class);
    variable.setResetType(JRVariable.RESET_TYPE_GROUP);
    JRDesignGroup group = new JRDesignGroup();
    group.setName("CityGroup");
    variable.setResetGroup(group);
    variable.setCalculation(JRVariable.CALCULATION_SYSTEM);
    JRDesignExpression expression = new JRDesignExpression();
    expression.setValueClass(java.lang.Integer.class);
    expression.setText("($V{CityNumber} != null)?(new Integer($V{CityNumber}.intValue() + 1)):(new Integer(1))");
    variable.setInitialValueExpression(expression);
    jasperDesign.addVariable(variable);

    variable = new JRDesignVariable();
    variable.setName("AllCities");
    variable.setValueClass(java.lang.String.class);
    variable.setResetType(JRVariable.RESET_TYPE_REPORT);
    variable.setCalculation(JRVariable.CALCULATION_SYSTEM);
    jasperDesign.addVariable(variable);

    //Groups
    group.setMinHeightToStartNewPage(60);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$F{City}");
    group.setExpression(expression);

    JRDesignBand band = new JRDesignBand();
    band.setHeight(20);
    JRDesignRectangle rectangle = new JRDesignRectangle();
    rectangle.setX(0);
    rectangle.setY(4);
    rectangle.setWidth(515);
    rectangle.setHeight(15);
    rectangle.setForecolor(new Color(0xC0, 0xC0, 0xC0));
    rectangle.setBackcolor(new Color(0xC0, 0xC0, 0xC0));
    band.addElement(rectangle);
    JRDesignTextField textField = new JRDesignTextField();
    textField.setX(0);
    textField.setY(4);
    textField.setWidth(515);
    textField.setHeight(15);
    textField.setBackcolor(new Color(0xC0, 0xC0, 0xC0));
    textField.setMode(JRElement.MODE_OPAQUE);
    textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
    textField.setStyle(boldStyle);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("\"  \" + String.valueOf($V{CityNumber}) + \". \" + String.valueOf($F{City})");
    textField.setExpression(expression);
    band.addElement(textField);
    JRDesignLine line = new JRDesignLine();
    line.setX(0);
    line.setY(19);
    line.setWidth(515);
    line.setHeight(0);
    band.addElement(line);
    group.setGroupHeader(band);

    band = new JRDesignBand();
    band.setHeight(20);
    line = new JRDesignLine();
    line.setX(0);
    line.setY(-1);
    line.setWidth(515);
    line.setHeight(0);
    band.addElement(line);
    JRDesignStaticText staticText = new JRDesignStaticText();
    staticText.setX(400);
    staticText.setY(0);
    staticText.setWidth(60);
    staticText.setHeight(15);
    staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
    staticText.setStyle(boldStyle);
    staticText.setText("Count : ");
    band.addElement(staticText);
    textField = new JRDesignTextField();
    textField.setX(460);
    textField.setY(0);
    textField.setWidth(30);
    textField.setHeight(15);
    textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
    textField.setStyle(boldStyle);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.Integer.class);
    expression.setText("$V{CityGroup_COUNT}");
    textField.setExpression(expression);
    band.addElement(textField);
    group.setGroupFooter(band);

    jasperDesign.addGroup(group);

    //Title
    band = new JRDesignBand();
    band.setHeight(50);
    line = new JRDesignLine();
    line.setX(0);
    line.setY(0);
    line.setWidth(515);
    line.setHeight(0);
    band.addElement(line);
    textField = new JRDesignTextField();
    textField.setBlankWhenNull(true);
    textField.setX(0);
    textField.setY(10);
    textField.setWidth(515);
    textField.setHeight(30);
    textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
    textField.setStyle(normalStyle);
    textField.setFontSize(22);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$P{ReportTitle}");
    textField.setExpression(expression);
    band.addElement(textField);
    jasperDesign.setTitle(band);

    //Page header
    band = new JRDesignBand();
    band.setHeight(20);
    rectangle = new JRDesignRectangle();
    rectangle.setX(0);
    rectangle.setY(5);
    rectangle.setWidth(515);
    rectangle.setHeight(15);
    rectangle.setForecolor(new Color(0x33, 0x33, 0x33));
    rectangle.setBackcolor(new Color(0x33, 0x33, 0x33));
    band.addElement(rectangle);
    staticText = new JRDesignStaticText();
    staticText.setX(0);
    staticText.setY(5);
    staticText.setWidth(55);
    staticText.setHeight(15);
    staticText.setForecolor(Color.white);
    staticText.setBackcolor(new Color(0x33, 0x33, 0x33));
    staticText.setMode(JRElement.MODE_OPAQUE);
    staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
    staticText.setStyle(boldStyle);
    staticText.setText("ID");
    band.addElement(staticText);
    staticText = new JRDesignStaticText();
    staticText.setX(55);
    staticText.setY(5);
    staticText.setWidth(205);
    staticText.setHeight(15);
    staticText.setForecolor(Color.white);
    staticText.setBackcolor(new Color(0x33, 0x33, 0x33));
    staticText.setMode(JRElement.MODE_OPAQUE);
    staticText.setStyle(boldStyle);
    staticText.setText("Name");
    band.addElement(staticText);
    staticText = new JRDesignStaticText();
    staticText.setX(260);
    staticText.setY(5);
    staticText.setWidth(255);
    staticText.setHeight(15);
    staticText.setForecolor(Color.white);
    staticText.setBackcolor(new Color(0x33, 0x33, 0x33));
    staticText.setMode(JRElement.MODE_OPAQUE);
    staticText.setStyle(boldStyle);
    staticText.setText("Street");
    band.addElement(staticText);
    jasperDesign.setPageHeader(band);

    //Column header
    band = new JRDesignBand();
    jasperDesign.setColumnHeader(band);

    //Detail
    band = new JRDesignBand();
    band.setHeight(20);
    textField = new JRDesignTextField();
    textField.setX(0);
    textField.setY(4);
    textField.setWidth(50);
    textField.setHeight(15);
    textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
    textField.setStyle(normalStyle);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.Integer.class);
    expression.setText("$F{Id}");
    textField.setExpression(expression);
    band.addElement(textField);
    textField = new JRDesignTextField();
    textField.setStretchWithOverflow(true);
    textField.setX(55);
    textField.setY(4);
    textField.setWidth(200);
    textField.setHeight(15);
    textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
    textField.setStyle(normalStyle);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$F{FirstName} + \" \" + $F{LastName}");
    textField.setExpression(expression);
    band.addElement(textField);
    textField = new JRDesignTextField();
    textField.setStretchWithOverflow(true);
    textField.setX(260);
    textField.setY(4);
    textField.setWidth(255);
    textField.setHeight(15);
    textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
    textField.setStyle(normalStyle);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$F{Street}");
    textField.setExpression(expression);
    band.addElement(textField);
    line = new JRDesignLine();
    line.setX(0);
    line.setY(19);
    line.setWidth(515);
    line.setHeight(0);
    line.setForecolor(new Color(0x80, 0x80, 0x80));
    line.setPositionType(JRElement.POSITION_TYPE_FLOAT);
    band.addElement(line);
    jasperDesign.setDetail(band);

    //Column footer
    band = new JRDesignBand();
    jasperDesign.setColumnFooter(band);

    //Page footer
    band = new JRDesignBand();
    jasperDesign.setPageFooter(band);

    //Summary
    band = new JRDesignBand();
    jasperDesign.setSummary(band)

    How to indert charts  into JasperReports

    JasperReports now has built-in support for charts. There is a new chart component ready to use like we already had images, text fields, subreports and other elements.

    This greatly simplifies the way charts are included inside reports, because previously the user had to completely rely on scriptlets in order to gather the chart data and render the chart using an image element in the report template.

    Now with the new chart component, the user only has to make the desired visual settings to it and define the expressions that will help the engine built-up the chart dataset in a incremental fashion during the iteration through the report data source.

    When including and configuring a chart component, there are three entities involved:
    - the overall chart component;
    - the chart dataset (groups chart data related settings);
    - the chart plot (groups visual settings related to the way the chart items are rendered);

    JasperReports currently supports the following types of charts:
    Pie, Pie 3D, Bar, Bar 3D, XY Bar, Stacked Bar, Stacked Bar 3D, Line, XY Line, Area, XY Area, Scatter Plot, Bubble, Time series, High Low Open Close, Candlestick.

    These types of charts use several types of datasets (each type of chart works with certain types of datasets): Pie Dataset, Category Dataset, XY Dataset, Time Series, Time Period Values, XYZ Dataset, High Low Dataset.

    For all charts we can configure the following:
    - border around all sides
    - background color
    - title
    - title position (top, left, bottom, right)
    - title font
    - title color
    - subtitle
    - subtitle color
    - subtitle font
    - show/hide legend
    - plot area background color
    - plot area background transparency (alpha)
    - plot area foreground transparency (alpha)
    - plot orientation (vertical, horizontal)
    - axis labels

    For all datasets we can configure:
    - increment type (detail, column, page, group, report)
    - increment group
    - reset type (none, column, page, group, report)
    - reset group

    Specific settings by chart type:

    Pie 3D
    - depth factor

    Bar, XY Bar, Stacked Bar
    - hide/show labels
    - hide/show tick marks
    - hide/show tick labels

    Bar 3D, Stacked Bar 3D
    - hide/show labels
    - x offset (3D effect)
    - y offset (3D effect)

    Line, XY Line, Scatter Plot, Time series
    - hide/show lines
    - hide/show shapes

    Bubble
    - scale type (both axes, domain axis, range axis)

    High Low Open Close
    - hide/show close ticks
    - hide/show open ticks

    Candlestick
    - hide/show volume

    JasperReports uses the JFreeChart library to render the charts and details about how to use this functionality can be found in the supplied "charts" sample

    */
    }
